﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SQL_Manager
{
    public partial class frmSaveConnection : Form
    {
        public frmSaveConnection(string username, string password, string database, string hostname, int driver)
        {
            InitializeComponent();
            //
            // Populate the form with pre-entered data
            //
            this.txtDisplayName.Text = username;
            this.txtUsername.Text = username;
            this.txtPassword.Text = password;
            this.txtDatabase.Text = database;
            this.txtHost.Text = hostname;
            this.cmbDBType.SelectedIndex = driver;

            //
            // Enable or disable the save button
            //
            this.txtUsername_TextChanged(null, null);
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            try
            {
                //
                // Disallow blank DisplayName -- set it to username if it's blank
                //
                if (this.txtDisplayName.Text.Length == 0)
                {
                    this.txtDisplayName.Text = this.txtUsername.Text;
                }

                //
                // Create a new profile and save it
                //
                Profile profile = new Profile
                {
                    name = this.txtDisplayName.Text,
                    category = this.txtCategory.Text,
                    database = this.txtDatabase.Text,
                    driver = (DriverType)this.cmbDBType.SelectedIndex,
                    hostname = this.txtHost.Text,
                    password = this.txtPassword.Text,
                    username = this.txtUsername.Text
                };

                ApplicationProfiles.insertProfile(profile);

                this.DialogResult = DialogResult.OK;
            }
            catch
            {
                this.DialogResult = DialogResult.Abort;
            }
        }

        //
        // Requires Username, Host and Database to be filled out
        //
        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            this.cmdSave.Enabled = (this.txtUsername.Text.Length > 0 && this.txtHost.Text.Length > 0 && this.txtDatabase.Text.Length > 0);
        }

        //
        // Requires Username, Host and Database to be filled out
        //
        private void txtDatabase_TextChanged(object sender, EventArgs e)
        {
            this.cmdSave.Enabled = (this.txtUsername.Text.Length > 0 && this.txtHost.Text.Length > 0 && this.txtDatabase.Text.Length > 0);
        }

        //
        // Requires Username, Host and Database to be filled out
        //
        private void txtHost_TextChanged(object sender, EventArgs e)
        {
            this.cmdSave.Enabled = (this.txtUsername.Text.Length > 0 && this.txtHost.Text.Length > 0 && this.txtDatabase.Text.Length > 0);
        }
    }
}
